//
//  ViewController.m
//  DownloaderTest
//
//  Created by user on 2018/6/13.
//  Copyright © 2018年 Dnake. All rights reserved.
//

#import "ViewController.h"

#import "TableViewCell.h"
#import "XDownloadModel.h"
#import "XDownloadManager.h"
#import "MJExtension.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, copy) NSArray *listArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *testData = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"testData.plist" ofType:nil]];
    
    _listArray = [XDownloadModel mj_objectArrayWithKeyValuesArray:testData];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"继续";
}

- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];
    
    XDownloadModel *model = _listArray[indexPath.row];
    
    cell.titleLabel.text = model.fileName;
    cell.subTitleLabel.text = [NSString stringWithFormat:@"进度：%f",model.progress];
    
    cell.block = ^(UIButton *sender) {
        [[XDownloadManager shareManager] pauseDownloadTask:model];
    };
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    XDownloadModel *model = _listArray[indexPath.row];
    
    TableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    [[XDownloadManager shareManager] startDownloadTask:model ProgressBlock:^(CGFloat progress) {
        dispatch_async(dispatch_get_main_queue(), ^{
            cell.subTitleLabel.text = [NSString stringWithFormat:@"进度：%f",progress];
        });
        
    }];
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    //在这里实现删除操作
    
    XDownloadModel *model = _listArray[indexPath.row];
    
    TableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [[XDownloadManager shareManager] continueDownloadTask:model ProgressBlock:^(CGFloat progress) {
        dispatch_async(dispatch_get_main_queue(), ^{
            cell.subTitleLabel.text = [NSString stringWithFormat:@"进度：%f",progress];
        });
        
    }];
}


@end
